package com.zhong.beautifulimageapp;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import com.zhong.beautifulimageapp.model.UserAfterLoginInfo;
import com.zhong.beautifulimageapp.utils.RequestPermissionInterface;

/**
 * 公用的方法放在该处
 * 目前包括的公共方法：1、动态申请权限
 *                     2、操作SharedPreferences来获取或存储数据
 * Created by 俊峰 on 2017/7/26.
 */

public class BaseActivity extends AppCompatActivity {

    /**
     * 申请权限成功后调用的接口
     */
    private RequestPermissionInterface mPermissionInterface;
    public static int REQUEST_FOR_PERMISSION_CODE = 123;
    public static int REQUEST_FOR_CAMERA_CODE = 101;

    public void setPermissionInterface(RequestPermissionInterface permissionInterface){
        mPermissionInterface = permissionInterface;
    }

    /**
     * 公用的申请权限方法
     * @param permissions 要申请的权限
     */
    public void dynamicApplication(String[] permissions){
        for (int i = 0; i < permissions.length; i++) {
            if (ActivityCompat.checkSelfPermission(this, permissions[i])
                    != PackageManager.PERMISSION_GRANTED){
                if (ActivityCompat.shouldShowRequestPermissionRationale(this, permissions[i])){
                    ActivityCompat.requestPermissions(this,
                            new String[]{permissions[i]},
                            REQUEST_FOR_PERMISSION_CODE);
                }else {
                    ActivityCompat.requestPermissions(this,
                            new String[]{permissions[i]},
                            REQUEST_FOR_PERMISSION_CODE);
                }
            }else {
                mPermissionInterface.success();
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == REQUEST_FOR_PERMISSION_CODE){
            if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                mPermissionInterface.success();
            }else {
                Toast.makeText(this, "必须授权才能使用该功能", Toast.LENGTH_SHORT).show();
            }
        }else {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }

    /**
     * 存储AccessToken
     * @param accessToken 获取的AccessToken
     */
    public void storageTheAccessToken(String accessToken){
        SharedPreferences userInfo = getSharedPreferences("UserInfo", Context.MODE_PRIVATE);
        if (userInfo != null) {
            SharedPreferences.Editor edit = userInfo.edit();
            edit.putString("AccessToken", accessToken);
            edit.apply();
        }
    }

    /**
     * 获取存储的AccessToken
     * @return 返回AccessToken
     */
    public String getTheAccessTokenFromStorage(){
        String token = null;
        SharedPreferences userInfo = getSharedPreferences("UserInfo", Context.MODE_PRIVATE);
        if (userInfo != null) {
            token =  userInfo.getString("AccessToken", "");
        }
        return token;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && data != null){
            //存储扫码得到的AccessToken
            if (requestCode == REQUEST_FOR_CAMERA_CODE){
                String result = data.getStringExtra("result");
                storageTheAccessToken(result);
                mPermissionInterface.successForCamera();
            }
        }
    }

    /**
     * 存储用户登录后服务器返回的用户简略信息
     * @param loginName 用户名
     * @param id 用户id
     * @param userIcon 用户头像的地址
     */
    public void setTheUserAfterLoginInfo(String loginName, String id, String userIcon){
        SharedPreferences userInfo = getSharedPreferences("UserInfo", Context.MODE_PRIVATE);
        if (userInfo != null) {
            SharedPreferences.Editor edit = userInfo.edit();
            edit.putString("loginName", loginName);
            edit.putString("id", id);
            edit.putString("userIcon", userIcon);
            edit.apply();
        }
    }

    /**
     * 获取存储的用户简略信息
     * @param userAfterLoginInfo 用户简略信息实体类
     */
    public void getTheUserAfterLoginInfo(UserAfterLoginInfo userAfterLoginInfo){
        SharedPreferences userInfo = getSharedPreferences("UserInfo", Context.MODE_PRIVATE);
        if (userInfo != null) {
            userAfterLoginInfo.setId(userInfo.getString("id", ""));
            userAfterLoginInfo.setLoginName(userInfo.getString("loginName", ""));
            userAfterLoginInfo.setUserIcon(userInfo.getString("userIcon", ""));
            userAfterLoginInfo.setSuccess(true);
        }
    }

    /**
     * 清除sharePreferences中存储的用户简略信息
     */
    public void clearTheUserAfterLoginInfo(){
        SharedPreferences userInfo = getSharedPreferences("UserInfo", Context.MODE_PRIVATE);
        if (userInfo != null) {
            SharedPreferences.Editor editor = userInfo.edit().clear();
            editor.apply();
        }
    }
}
